package com.example.a2306_shop.address

import androidx.lifecycle.viewModelScope
import com.example.lib_base.entity.AddressEditEntity
import com.example.lib_base.entity.AddressEntity
import com.example.lib_base.entity.Res
import com.example.lib_base.mvvm.BaseViewModel
import com.example.lib_base.param.AddressEdit
import com.example.lib_base.utils.SingleLiveData
import kotlinx.coroutines.launch

class AddressViewModel:BaseViewModel() {
    //请求地址列表
    val addressSuccess = SingleLiveData<Res<AddressEntity>>()
    val addressFail = SingleLiveData<String>()
    fun address(limit:Int,page:Int){
        viewModelScope.launch {
            try {
                addressSuccess.postValue(apiService.address(limit, page))
            } catch (e: Exception) {
                addressFail.postValue(e.message.toString())
            }
        }
    }
    //保存地址信息
    val addressEditSuccess = SingleLiveData<Res<AddressEditEntity>>()
    val addressEditFail = SingleLiveData<String>()
    fun addressEdit(addressEdit: AddressEdit){
        viewModelScope.launch {
            try {
                addressEditSuccess.postValue(apiService.addressEdit(addressEdit))
            } catch (e: Exception) {
                addressEditFail.postValue(e.message.toString())
            }
        }
    }
    //地址详情
    val addressDetailSuccess = SingleLiveData<Res<AddressEditEntity>>()
    val addressDetailFail = SingleLiveData<String>()
    fun addressDetail(id:Int){
        viewModelScope.launch {
            try {
                addressDetailSuccess.postValue(apiService.addressDetail(id))
            } catch (e: Exception) {
                addressDetailFail.postValue(e.message.toString())
            }
        }
    }
}